﻿using System;
using System.Windows.Forms;
using SpeechLib;
using graduation_project.Voice_Recognition;


namespace graduation_project
{    
    public partial class SpeechMain : Form
    {
        public SimulationMain SimulMain;
        public KeyWordsMain KeyMain;
        private SpVoice Voice;
        ComSet com = new ComSet();
        System.Windows.Forms.Timer comTime = new System.Windows.Forms.Timer();
        ExecuteCmd executeCmd = new ExecuteCmd();
        public SpeechMain()
        {
            InitializeComponent();
        }
        private void SpeechMain_Load(object sender, EventArgs e)
        {
            Voice = new SpVoice();
            //this.com.initializeSpeech();
        }
        /// <summary>
        /// 文本识别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextToSpeechBtn_Click(object sender, EventArgs e)
        {
            comTime.Stop();
            com.ComSetInitialize();
            com.shareRecoGrammar.DictationSetState(SpeechRuleState.SGDSInactive);
            if (this.MainTextBox.Text == "")
            {
                MessageBox.Show("请输入文字", "Speech", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                Voice.Speak(MainTextBox.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);             
        }
        /// <summary>
        /// 语音识别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SpeechToTextBtn_Click(object sender, EventArgs e)
        {
            comTime.Stop();
           if (MainTextBox.Text != null) 
           {
               MainTextBox.Clear();
           }
           com.ComSetInitialize();
           com.BeginRec(MainTextBox);
        }
        
        /// <summary>
        /// 命令识别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdBtn_Click(object sender, EventArgs e)
        {
            if (com.shareRecoGrammar != null)
            {
                com.closeRec();
            }
            MainTextClear();
            comTime.Start();
            comTime.Enabled = true;
            comTime.Interval = 1;
            comTime.Tick += new EventHandler(comTime_Tick);
            #region 多线程方法！
            //com.ComSetInitialize();
            //com.BeginRec(MainTextBox);
            //Thread CmdRecongnition = new Thread(delegate()
            //{
            //    Thread.Sleep(7000);
            //    ExecuteCmd executeCmd = new ExecuteCmd();
            //    switch (MainTextBox.Text)
            //    {
            //        case "打开记事本":
            //            executeCmd.ExeCommand("notepad");
            //            break;
            //        case "打开画图板":
            //            executeCmd.ExeCommand("mapaint");
            //            break;
            //        case "打开写字板":
            //            executeCmd.ExeCommand("write");
            //            break;
            //        case "查看系统版本":
            //            executeCmd.ExeCommand("winver");
            //            break;
            //        case "打开播放器":
            //            executeCmd.ExeCommand("dvdplay");
            //            break;
            //        case "启动播放器":
            //            executeCmd.ExeCommand("dvdplay");
            //            break;
            //        case "计算器":
            //            executeCmd.ExeCommand("calc");
            //            break;

            //    }

            //});
            //CmdRecongnition.Start();
            #endregion
        }

        void comTime_Tick(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            //ExecuteCmd executeCmd = new ExecuteCmd();
            switch (MainTextBox.Text)
            {
                case "打开记事本":
                    { 
                        executeCmd.ExeCommand("notepad");
                        MainTextClear();
                    }
                    
                    break;
                case "打开画图板":
                    {
                        executeCmd.ExeCommand("mapaint");
                        MainTextClear();
                    }
                    break;
                case "打开写字板":
                    {
                        executeCmd.ExeCommand("write");
                        MainTextClear();
                    }
                    break;
                case "查看系统版本":
                    {
                        executeCmd.ExeCommand("winver");
                        MainTextClear();
                    }
                    break;
                case "打开播放器":
                    {
                        executeCmd.ExeCommand("dvdplay");
                        MainTextClear();
                    }
                    break;
                case "启动播放器":
                    {
                        executeCmd.ExeCommand("dvdplay");
                        MainTextClear();
                    }
                    break;
                case "计算器":
                    {
                        executeCmd.ExeCommand("calc");
                        MainTextClear();
                    }
                    break;
            }
            
        }
        /// <summary>
        /// 清空并识别
        /// </summary>
        public void MainTextClear()
        {
            if (MainTextBox.Text != null)
            {
                MainTextBox.Clear();
            }
            com.ComSetInitialize();
            com.BeginRec(MainTextBox);
        }

        /// <summary>
        /// 模拟识别主窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SimulationBtn_Click(object sender, EventArgs e)
        {
            comTime.Stop();
            if (com.shareRecoGrammar != null)
            {
                com.closeRec();
            }
            this.Visible = false;
            SimulMain = new SimulationMain();
            SimulMain.ShowDialog();    //显示为dialog模式,当窗体关闭时，主窗体显示。
            this.Visible = true;            
        }

        private void KeyWordsButton_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            KeyMain = new KeyWordsMain();
            KeyMain.ShowDialog();
            this.Visible = true;
            
        }
    }
}
